import requests
import jsonpath
import csv
import pandas as pd

from msgs.msg import info


url = 'https://shuyang.anjuke.com/v3/ajax/map/sale/1828/prop_list/?room_num=-1&price_id=-1&area_id=-1&floor=-1&orientation=-1&is_two_years=0&is_school=0&is_metro=0&order_id=0&p=1&zoom=12&lat=34.084213_34.150682&lng=118.368882_119.251952&kw=&maxp=99&et=b66558&ib=1&bst=pem854'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
}


r = requests.get(url, headers=headers)
r.encoding = 'utf-8'
# print(r.content.decode())


# with open('./msgs/msg.py', 'w+',encoding='utf-8') as f:
#     f.write(item)

js1 = jsonpath.jsonpath(info, '$.val.props[*].region_name')
js2 = jsonpath.jsonpath(info, '$.val.props[*].block_name')
js3 = jsonpath.jsonpath(info, '$.val.props[*].rhval')
js4 = jsonpath.jsonpath(info, '$.val.props[*].area')
js5 = jsonpath.jsonpath(info, '$.val.props[*].price')
js6 = jsonpath.jsonpath(info, '$.val.props[*].long_title')
js7 = jsonpath.jsonpath(info, '$.val.props[*].comm_name')
js8 = jsonpath.jsonpath(info, '$.val.props[*].house_orient_name')
js9 = jsonpath.jsonpath(info, '$.val.props[*].floor_tag')
#

rows = []
for x in range(len(js1)):
    row = {
        'region_name': js1[x],
        'block_name': js2[x],
        'rhval': js3[x],
        'area': js4[x],
        'price': js5[x],
        'long_title': js6[x],
        'comm_name': js7[x],
        'house_orient_name': js8[x],
        'floor_tag': js9[x]
    }

    rows.append(row)

with open('text2.csv', 'w', encoding='utf-8-sig') as f:
    f_csv = csv.DictWriter(f, ['region_name', 'block_name', 'rhval', 'area', 'price', 'long_title', 'comm_name', 'house_orient_name', 'floor_tag'])
    f_csv.writeheader()
    f_csv.writerows(rows)
# with open('save_js.csv', 'w', encoding='utf-8') as f:
#     w = csv.writer(f)
#     w.writerow(['region_name', 'block_name', 'rhcal', 'area', 'price', 'long_title', 'comm_name', 'house_orient_name', 'floor_tag'])
#     w.writerow(js1)

